<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

/**
* This controller is used for JAEmail feature of the component
*/
class JAJobBoardControllerJAMail extends JAJobBoardController {
/**
     * Constructor
     */
    function __construct( $location = array() )
    {
    	JRequest::setVar('name','jamail');
    	parent::__construct();
    }
    
    /**
    * Display current jamail of the component to administrator
    * 
    */
    function display()
    {    
    	JRequest::setVar( 'layout', 'form' );
		JRequest::setVar( 'hidemainmenu', 1 );
		parent::display();
    }         
   
    function preview()
    {
    	// Initialize variables
		$document		=& JFactory::getDocument();
		$db 			=& JFactory::getDBO();
		$id				= JRequest::getVar( 'id', 0, '', 'int' );
		$option			= JRequest::getCmd( 'option' );

		// Get the current default template
		$query = 'SELECT template' .
				' FROM #__templates_menu' .
				' WHERE client_id = 0' .
				' AND menuid = 0';
		$db->setQuery($query);
		$template = $db->loadResult();

		// check if template editor stylesheet exists
		if (!file_exists( JPATH_SITE.DS.'templates'.DS.$template.DS.'css'.DS.'editor.css' )) {
			$template = 'system';
		}

		// Set page title
		$document->setTitle(JText::_('Article Preview'));
		$document->addStyleSheet(JURI::root() . 'templates/'.$template.'/css/editor.css');
		$document->setBase(JUri::root());
		parent::display();
    }
    
	/**
	* Save categories record
	*/
	function send()
	{
		global $option;
		$admin_user = JFactory::getUser();
		$model = $this->getModel('jamail');
		$data = $model->getData();
		$arr_user_id = JRequest::getVar('user_id');
		$arr_user_id = explode(',',$arr_user_id);
		$arr_email = explode(',',$data['emails']);
		for ($i=0;$i<count($arr_email);$i++)
		{
			$user = JFactory::getUser($arr_user_id[$i]);
			$updated_content = $model->updateMailContent($arr_user_id[$i],$data['content']);
			$temp = sendmail($arr_email[$i],$user->name,$data['subject'],$updated_content,'',$data['email_from'],$admin_user->name);
			if ($temp)
				$msg = JText::_('Sending mail succesfully');
//			else 		
//				$msg = JText::_('Error sending mail');	
		}
		$this->setRedirect("index.php?option=$option&view=".JRequest::getVar('pre_view'),$msg);
	}			
}